var gulp = require('gulp'),
    htmlmini = require('gulp-html-minify'),
    useref = require('gulp-useref'),
    uglify = require('gulp-uglify'),
    csso = require('gulp-csso'),
    filter = require('gulp-filter'),
    RevAll = require('gulp-rev-all'),
    concat = require('gulp-concat'),
    prefixer = require('gulp-autoprefixer'),
    del = require('del'),
    less = require('gulp-less'),
    rev = require('gulp-rev-append'),
    rename = require('gulp-rename'),
    gutil = require('gulp-util');

gulp.task('rev', function () {
    //不删除的话   hash的版本号不更新
    del([
        './index.html.online'
    ]);
    gulp.src('./index-docs.html')
        .pipe(rev())
        //上线时使用index.html.onlien  上线脚本会自动替换成index.html
        .pipe(rename('index.html.online'))
        // .pipe(rename('index.html'))
        .pipe(gulp.dest('./'));
});

var pluginsJs = [
    'libs/jquery/jquery/dist/jquery.js',
    'libs/angular/angular/angular.1.5.9.min.js',
    'libs/angular/angular-animate/angular-animate.1.5.9.min.js',
    'libs/angular/angular-aria/angular-aria.1.5.9.min.js',
    'libs/angular/angular-cookies/angular-cookies.1.5.9.min.js',
    'libs/angular/angular-messages/angular-messages.1.5.9.min.js',
    'libs/angular/angular-resource/angular-resource.1.5.9.min.js',
    'libs/angular/angular-sanitize/angular-sanitize.1.5.9.min.js',
    'libs/angular/angular-touch/angular-touch.1.5.9.min.js',
    'libs/angular/angular-diff-match-patch/diff_match_patch.js',
    'libs/angular/angular-material/angular-material.js',
    'libs/angular/angular-ui-router/release/angular-ui-router.min.js',
    'libs/angular/ngstorage/ngStorage.js',
    'libs/angular/angular-ui-utils/ui-utils.js',
    'libs/angular/angular-bootstrap/ui-bootstrap-tpls.js',
    'libs/angular/oclazyload/dist/ocLazyLoad.js',
    'libs/angular/angular-translate/angular-translate.js',
    'libs/angular/angular-translate-loader-static-files/angular-translate-loader-static-files.js',
    'libs/angular/angular-translate-storage-cookie/angular-translate-storage-cookie.js',
    'libs/angular/angular-translate-storage-local/angular-translate-storage-local.js',
    'libs/angular/angular-file-upload/angular-file-upload.js',
    'libs/angular/angularjs-toaster/toaster.js',
    'js/plugins/lodash.min.js',
    'js/fileInput/ng-file-upload.min.js',
    'js/fileInput/ng-file-upload-shim.min.js',
    'js/fileInput/select.min.js',
    'js/fileInput/fileinput.min.js',
    'js/fileInput/zh.js',
    'libs/angular/angular-multi-select-tree/angular-multi-select-tree-0.1.0.js',
    'libs/angular/angular-multi-select-tree/angular-multi-select-tree-0.1.0.tpl.js',
    'libs/angular/angular-smart-table/dist/smart-table.min.js',
    'js/websocket/stomp.min.js',
    'js/websocket/sockjs.min.js',
    'js/websocket/websocket.js'
];

gulp.task('concat-plugins-js', function () {
    gulp.src(pluginsJs)
        .pipe(concat('plugins.min.js'))
        .pipe(uglify({
            mangle: false //类型：Boolean 默认：true 是否修改变量名
        }))
        .pipe(gulp.dest('js'));
});

var pluginBootstrapJs = [
    'js/directives/setnganimate.js',
    'js/directives/ui-butterbar.js',
    'js/directives/ui-focus.js',
    'js/directives/ui-fullscreen.js',
    'js/directives/ui-jq.js',
    'js/directives/ui-module.js',
    'js/directives/ui-nav.js',
    'js/directives/ui-scroll.js',
    'js/directives/ui-shift.js',
    'js/directives/ui-toggleclass.js',
    'js/controllers/bootstrap.js'
];

gulp.task('concat-plugin-bootstrap-js', function () {
    gulp.src(pluginBootstrapJs)
        .pipe(concat('plugin-bootstrap.min.js'))
        .pipe(uglify({
            mangle: false //类型：Boolean 默认：true 是否修改变量名
        }))
        .pipe(gulp.dest('js'));
});

var pluginFroalarapJs = [
    'js/froala/froala_editor.min.js',
    'js/froala/angular-froala.js',
    'js/froala/froala-sanitize.js',
    'js/froala/colors.min.js',
    'js/froala/char_counter.min.js',
    'js/froala/table.min.js',
    'js/codemirror/codemirror.min.js',
    'js/codemirror/xml.min.js',
    'js/froala/froala_editor.pkgd.min.js'
];

gulp.task('concat-froala-bootstrap-js', function () {
    gulp.src(pluginFroalarapJs)
        .pipe(concat('plugin-froala.min.js'))
        .pipe(uglify({
            mangle: false //类型：Boolean 默认：true 是否修改变量名
        }))
        .pipe(gulp.dest('js'));
});

var matrixBaseJs = [
    'js/app.js',
    'js/config.js',
    'data/data.js',
    'js/config.lazyload.js',
    'js/config.router.js',
    'js/main.js',
    'js/services/http-load.js',
    'js/services/ui-load.js',
    'js/filters/fromNow.js'
];

gulp.task('concat-matrix-base-js', function () {
    gulp.src(matrixBaseJs)
        .pipe(concat('matrix-base.min.js'))
        .pipe(uglify({
            mangle: false //类型：Boolean 默认：true 是否修改变量名
        }))
        .pipe(gulp.dest('js'));
});

var matrixJs = [
    'matrix/common/common.js',
    'matrix/component/controller/winitTable.js',
    'matrix/component/controller/winitEditTable.js',
    'matrix/component/controller/mTree.js',
    'matrix/component/controller/winitInputGroup.js',
    'matrix/component/controller/winitIAdd.js',
    'matrix/component/controller/winitPdmNeed.js',
    'matrix/component/controller/winitDepartmentNeed.js',
    'matrix/component/controller/winitIssueNeed.js',
    'matrix/component/controller/winitBasicsNeed.js',
    'matrix/component/controller/component.js',
    'matrix/component/controller/mDatepicker.js',
    'matrix/component/controller/mForm.js',
    'matrix/component/controller/mWysiwyg.js',
    'matrix/component/controller/mMenuTree.js',
    'matrix/component/controller/mPageTree.js',
    'matrix/component/controller/mFunTree.js',
    'matrix/component/controller/confirm.js',
    'matrix/component/controller/gridDialog.js',
    'matrix/component/controller/confirmPop.js',
    'matrix/component/controller/confirmEditTree.js',
    'matrix/component/controller/addChildEle.js',
    'matrix/component/controller/saveLayoutWindow.js',
    'matrix/component/controller/winitFeaturePanel.js',
    'matrix/component/controller/winitFlowPanel.js',
    'matrix/component/controller/winitFeatureMap.js',
    'matrix/component/controller/winitFile.js',
    'matrix/component/controller/winitHistoryFeatureMap.js',
    'matrix/component/controller/winitMindMap.js',
    'matrix/component/controller/winitFlowMap.js',
    'matrix/component/controller/winitFlowBuilder.js',
    'matrix/component/controller/winitPrototypePanel.js',
    'matrix/component/controller/winitPrototype.js',
    'matrix/component/controller/createReqDesign.js',
    'matrix/component/controller/mProjectPlan.js',
    'matrix/component/controller/createProjectPlan.js',
    'matrix/component/controller/taskSave.js',
    'matrix/component/controller/winitSort.js',
    'matrix/component/controller/winitTransform.js'
];


gulp.task('concat-matrix-js', function () {
    gulp.src(matrixJs)
        .pipe(concat('matrix.min.js'))
        .pipe(uglify({
            mangle: false //类型：Boolean 默认：true 是否修改变量名
        }))
        .on('error', function (err) {
          gutil.log(gutil.colors.red('[Error]'), err.toString());
        })
        .pipe(gulp.dest('js'));
});

var pluginsCss = [
    'libs/jquery/bootstrap/dist/css/bootstrap.css',
    'libs/assets/animate.css/animate.css',
    'libs/assets/font-awesome/css/font-awesome.min.css',
    'libs/assets/simple-line-icons/css/simple-line-icons.css',
    'libs/angular/angularjs-toaster/toaster.css',
    'css/font.css',
    'css/app.css',
    'css/select.min.css',
    'css/fileinput.min.css',
    'css/froala_editor.min.css',
    'css/froala_style.min.css',
    'css/colors.min.css',
    'css/char_counter.min.css',
    'css/table.min.css',
    'css/froala_editor.pkgd.min.css',
    'libs/angular/angular-multi-select-tree/angular-multi-select-tree-0.1.0.css'
];

gulp.task('concat-plugins-css', function () {
    gulp.src(pluginsCss)
        .pipe(concat('plugins.min.css'))
        .pipe(csso())
        .pipe(gulp.dest('css'));
});

gulp.task('default', ['concat-plugins-css', 'concat-plugins-js', 'concat-plugin-bootstrap-js', 'concat-froala-bootstrap-js', 'concat-matrix-base-js', 'concat-matrix-js', 'rev']);  //默认压缩css + js   命令：gulp

